var queingAlarm = 0;
var openObj;

function alarm(str){
	var htmlStr = "<div><div style='margin:2px; padding: .7em .7em;'>"; 
	htmlStr += "<p><span style='float: left; margin-right: .3em;'></span>";
	htmlStr += "<strong>"+str+"</strong></p></div></div>";
	$("#alarmLayer").html(htmlStr).hide();
	$("#alarmLayer").slideDown(200);
	queingAlarm++;
	setTimeout('closeAlarm()',2300);
}

function Popup(url,target,flag) 
{
	if(flag!=null)
		openObj = window.open(url,target,flag,false);
	else
		openObj = window.open(url,target,"menubar=no,width=560,height=460,toolbar=no,resizable=no",false);
}

function closeAlarm(){
	if(--queingAlarm==0){
		$("#alarmLayer").slideUp(500);
	}
}

function UTF8Encoder(str){
	str = encodeURIComponent(str);
	str=str.replace(new RegExp('\\+','g'),'%2B');
	return str.replace(new RegExp('%20','g'),'+');
}

function UTF8Decoder(str){
    str=str.replace(new RegExp('\\+','g'),' ');
    return decodeURIComponent(str);
}

function getStyleCoverFlow(){
	$("#firstIssueCoverFlow").ready(function(){
		var flashvars = false;
		var params = {};
		var attributes = {
		  id: "styleCoverFlow",
		  name: "styleCoverFlow"
		};
	
		swfobject.embedSWF("flash/StyleCoverFlow.swf", "firstIssueCoverFlow", "700", "300", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	});
}
var loading = "<div id='loader'><image src='images/loader.gif'/></div>";

$(document).ready(function(){
	
});

function changeMenu(targetMenu){
	if(targetMenu=="Style" || targetMenu=="About" || targetMenu=="Community" || targetMenu=="Shop" || targetMenu=="Notice"){
		$("#bodyLeftMenuLayer").css("width","150px").hide();
		$("#bodyLeftMenuLayer").fadeIn(500);
		$("#bodyContentLayer").css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="JoinMember"){
		$("#bodyLeftMenuLayer").fadeOut(200).css("width","0px");
		$("#bodyContentLayer").load("jsp/body/regMember.jsp").css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}
	
}

function getBoardListByCategory(curItem,category,targetMenu){
	$("#bodyLeftMenuLayer ul").children(".leftMenuSelectedLi").removeClass("leftMenuSelectedLi");
	$(curItem).parent().attr("class","leftMenuSelectedLi");
	if(targetMenu == "Community"){
		$("#bodyContentLayer").load("jsp/body/community.jsp?category_seq="+category).hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Style"){
		$("#bodyContentLayer").load("jsp/body/style.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Shop"){
		$("#bodyContentLayer").load("jsp/body/shop.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="About"){
		$("#bodyContentLayer").load("jsp/body/about.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Notice"){
		$("#bodyContentLayer").load("jsp/body/notice.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}
}

function getBoardListByJustCategory(category,targetMenu){
	if(targetMenu == "Community"){
		$("#bodyContentLayer").load("jsp/body/community.jsp?category_seq="+category).hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Style"){
		$("#bodyContentLayer").load("jsp/body/style.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Shop"){
		$("#bodyContentLayer").load("jsp/body/shop.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="About"){
		$("#bodyContentLayer").load("jsp/body/about.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Notice"){
		$("#bodyContentLayer").load("jsp/body/notice.jsp?category_seq="+category).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}
}

function getBoardListByJustCategoryPage(category,targetMenu,page){
	if(!page || page == '')page=$("#setPage").val();
	
	if(page<1)page=1;
	else if(parseInt(page)>parseInt($("#maxPage").val()))page = $("#maxPage").val();
	
	if(targetMenu == "Community"){
		$("#bodyContentLayer").load("jsp/body/community.jsp?category_seq="+category+"&p="+page).hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Style"){
		$("#bodyContentLayer").load("jsp/body/style.jsp?category_seq="+category+"&p="+page).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Shop"){
		$("#bodyContentLayer").load("jsp/body/shop.jsp?category_seq="+category+"&p="+page).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="About"){
		$("#bodyContentLayer").load("jsp/body/about.jsp?category_seq="+category+"&p="+page).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}else if(targetMenu=="Notice"){
		$("#bodyContentLayer").load("jsp/body/notice.jsp?category_seq="+category+"&p="+page).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
		$("#bodyContentLayer").fadeIn(500);
	}
	
	setTimeout(setPageFocus,1000);
	return false;
}
function setPageFocus(){
	if($("#setPage")){
		$("#setPage").focus();
	}
}

function tryLogout(){
	location.href="jsp/commons/logoutProc.jsp";
}

function getLoginForm(){
	$("#loginBtn").hide();
	$("#loginForm").fadeIn(500);
	$("#login_id").focus();
}
function getRegForm(){
	location.href="#JoinMember";
	changeMenu("JoinMember");
}
function checkRegForm(){
	if(!$("#reg_name").val()){
		alarm('이름을 입력하세요');
		$("#reg_name").focus();
		return false;
	}
	if(!$("#reg_id").val()){
		alarm('아이디를 입력하세요');
		$("#reg_id").focus();
		return false;
	}
	if(!$("#reg_nick").val()){
		alarm('닉네임을 입력하세요');
		$("#reg_nick").focus();
		return false;
	}
	if(!$("#reg_password").val()){
		alarm('비밀번호를 입력하세요');
		$("#reg_password").focus();
		return false;
	}
	if(!$("#reg_password_confirm").val() || $("#reg_password").val()!=$("#reg_password_confirm").val()){
		alarm('비밀번호는 일치해야합니다.');
		$("#reg_password_confirm").focus();
		return false;
	}
	if(!$("#reg_email").val()){
		alarm('이메일을 입력하세요');
		$("#reg_email").focus();
		return false;
	}
	if(!$("#reg_mobile1").val()){
		alarm('휴대폰 번호를 입력하세요');
		$("#reg_mobile1").focus();
		return false;
	}
	if(!$("#reg_mobile2").val()){
		alarm('휴대폰 번호를 입력하세요');
		$("#reg_mobile2").focus();
		return false;
	}
	if(!$("#reg_mobile3").val()){
		alarm('휴대폰 번호를 입력하세요');
		$("#reg_mobile3").focus();
		return false;
	}
	return true;
}

function getRegMemResult(result){
	jsonObj = JSON.parse(result, function (key, value) {
	    var type;
	    if (value && typeof value === 'object') {
	        type = value.type;
	        if (typeof type === 'string' && typeof window[type] === 'function') {
	            return new (window[type])(value);
	        }
	    }
	    return value;
	});
	$("#btnRegMember").attr("disabled","");
	
	alarm(jsonObj.ERROR_MSG);
	
	if(jsonObj.RESULT=="success"){
		changeMenu("Home");
		$("#login_id").focus();
	}
}

function checkLoginForm(){
	if(!$("#login_id").val() || $("#login_id").val()=="아이디"){
		alarm('아이디를 입력하세요');
		$("#login_id").focus();
		return false;
	}
	if(!$("#login_pw").val()){
		alarm('비밀번호를 입력하세요');
		$("#login_pw").focus();
		return false;
	}
	return true;
}

function afterTryLogin(result){
	if(result=='success'){
		$("#loginLayer").load("jsp/header/loginForm.jsp").hide();
		$("#loginLayer").fadeIn(500);
		alarm("로그인 성공! :) 반갑습니다~");
	}else{
		alarm("로그인에 실패하였습니다.");
	}
		
}

function modalDialogOpen(comurl,rgParam)
{
	var sFeatures = "dialogLeft: 400px; dialogTop: 185px; dialogWidth:700px; dialogHeight:500px; status:no;help:no";
	var szResult = showModalDialog( comurl, rgParam, sFeatures );
	return szResult;
}

function getPostCodeWindow()
{
	var result1, result2;
	var rgParam = new Array;
	var retVal = modalDialogOpen('jsp/commons/zipcode.jsp',rgParam);
	if(retVal !=null){
	    var returnVal = new Array;
	    returnVal = retVal;
	    $("#reg_zipcode1").val(returnVal[0].substring(0,3));
	    $("#reg_zipcode2").val(returnVal[0].substring(4,7));
	    $("#reg_addr1").val(returnVal[1]);
	}
	return;
}

function readBoard(seq){
	$("#bodyContentLayer").load("jsp/commons/readForm.jsp?board_seq="+seq).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
	$("#bodyContentLayer").fadeIn(500);
}

function writeBoard(curmenu){

	$("#bodyContentLayer").load("jsp/commons/writeForm.jsp?curmenu="+curmenu+"&category_seq="+$("#curCategorySeq").val()).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
	$("#bodyContentLayer").fadeIn(500);
}

function writeChild(curmenu,parent){

	$("#bodyContentLayer").load("jsp/commons/writeForm.jsp?parent="+parent+"&curmenu="+curmenu+"&category_seq="+$("#curCategorySeq").val()).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
	$("#bodyContentLayer").fadeIn(500);
}
function updateBoard(seq){
	$("#bodyContentLayer").load("jsp/commons/updateForm.jsp?board_seq="+seq).css("width",($("#bodyLeftSide").width()-10-$("#bodyLeftMenuLayer").width())+"px").hide();
	$("#bodyContentLayer").fadeIn(500);
}

function afterWriteComplete(curCategory,result,curMenu){
	if(result.length>0 && parseInt(result)>0){
		alarm("저장했습니다.");
		getBoardListByJustCategory(curCategory,curMenu);
	}else{
		alarm("저장실패! 잠시 후 다시 시도해주세요.");
	}
	
}

function checkWriteForm(){
	oEditors.getById["board_content"].exec("UPDATE_IR_FIELD", []);
	
	// 에디터의 내용에 대한 값 검증은 이곳에서 document.getElementById("ir1").value를 이용해서 처리하면 됩니다.

	
	$("#boardWriteBtn").attr("disabled","disabled");
	
	if(!$.trim($("#write_category_seq").val())){
		alarm("필요로하는 값이 존재하지 않습니다.");
		$("#boardWriteBtn").attr("disabled","");
		return false;
	}
	if(!$.trim($("#write_board_title").val())){
		alarm("제목을 입력하세요.");
		$("#write_board_title").focus();
		$("#boardWriteBtn").attr("disabled","");
		return false;
	}
	if(!$.trim($("#board_content").val()) || $.trim($("#board_content").val())=='<br>'){
		alarm("내용을 입력하세요.");
		$("#board_content").focus();
		$("#boardWriteBtn").attr("disabled","");
		return false;
	}
	
	$("#write_board_title").val( UTF8Encoder( $.trim($("#write_board_title").val()) ) );
	$("#board_content").val( UTF8Encoder( $.trim($("#board_content").val()) ) );
	
	return true;
}


function getReplyListByJustSeqPage(boardSeq,page){
	if(!page || page == '')page=$("#reply_setPage").val();
	
	if(page<1)page=1;
	else if(parseInt(page)>parseInt($("#reply_maxPage").val()))page = $("#reply_maxPage").val();
	
	if(isNaN(parseInt(page))){alarm('숫자만입력하세요.');return false;
	}
	
	$("#replyListContent").load("jsp/commons/replyList.jsp?board_seq="+boardSeq+"&p="+page).hide();
	$("#replyListContent").fadeIn(500);
	
	return false;
}


function writeCallReply(boardSeq){
	$("#replyWriteLayer").load("jsp/commons/replyWriteForm.jsp?board_seq="+boardSeq).hide();
	$("#replyWriteLayer").fadeIn(500);
	$("#reply_input_content").focus();
}

function replyFormCheck(){
	
	if(!$.trim($("#reply_input_content").val())){
		alarm("내용을 입력하세요.");
		$("#reply_input_content").focus();
		return false;
	}
	
	$("#reply_write_btn").attr("disabled","disabled");
	$("#reply_input_content").val(UTF8Encoder( $.trim( $("#reply_input_content").val() )));
	
	return true;
}

function afterReplyWriteComplete(boardSeq,result){
	if(result=='1'){
		alarm("저장했습니다.");
		getReplyListByJustSeqPage(boardSeq,1);
		$("#reply_input_content").val("");
		$("#reply_write_btn").attr("disabled","");
	}else{
		$("#reply_input_content").attr("disabled","").val( UTF8Decoder($("#reply_input_content").val()));
		$("#reply_write_btn").attr("disabled","");
		alarm("저장실패! 잠시 후 다시 시도해주세요.");
	}
}

function imgUploadComplete(imgsrc){
	sHTML = "<img src='"+imgsrc+"'>";
	oEditors.getById["board_content"].exec("PASTE_HTML", [sHTML]);
}

function checkSearchQuery(){
	if(!$.trim($("#searchQuery").val()) && $.trim($("#searchQuery").val()).length < 1){
		$("#searchQuery").focus();
		return false;
	}
	
	$("#searchQuery").val(UTF8Decoder($.trim($("#searchQuery").val())));

	return true;
}

function setCurGoodsItem(obj){
	alarm($(obj).val());
	if($(obj).val()!="0")$("#goods_seq").val($(obj).val());
	
}

function callTenderForm(goodsSeq){
	Popup("../Tender?goods_seq="+goodsSeq, "");
}
